<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$return=array();
$return['basic']=$this->plugin_setting('basic');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='main'){
	
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	//查询进入该页面的客户所有订单
	$params=array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status']) && $_RQ['status'] != 10){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}
	$order_list=pdo_getall('jy_bdd_order',$params,'','','createtime desc');
	if($order_list){
		foreach($order_list as &$item){
			$user=pdo_get('jy_bdd_user',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
			$serve=pdo_get('jy_bdd_lesson',array('id'=>$item['lesson_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['lesson']=pdo_get('jy_bdd_lesson',array('id'=>$item['lesson_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);			
		}
	}
	$return['order_list']=$order_list;
	exi($return);
}
//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
	exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_bdd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	$user=pdo_get('jy_bdd_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	//项目信息
	$order['lesson']=pdo_get('jy_bdd_lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['lesson_id']));	
	//技师
	$worker=pdo_get('jy_bdd_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['worker_id']));	
	
	$return['order']=$order;
	$return['user']=$user;
	$return['member']=$member;
	$return['worker']=$worker;
	exi($return);
}
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_bdd_order',array('status'=>'4'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//确认订单后，计算员工佣金,存入佣金日志表，并更新员工表员工总佣金
	$worker_id=pdo_getcolumn('jy_bdd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'worker_id');
	$pay=pdo_getcolumn('jy_bdd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'pay');
	$lev_id=pdo_getcolumn('jy_bdd_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id),'lev');
	$rate=pdo_getcolumn('jy_bdd_lev',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lev_id),'rate');
	if($rate){
		$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'worker_id'=>$worker_id,
		'order_id'=>$_RQ['id'],
		'money'=>$pay*$rate/100,
		'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_bdd_commission_log',$insert);
		$commission=pdo_getcolumn('jy_bdd_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id),'commission');
		$commission=$commission+$pay*$rate/100;
		pdo_update('jy_bdd_worker',array('commission'=>$commission),array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id));
	}
	
	exi('确认服务成功');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_bdd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除预约成功');
}
if($_RQ['op']=='pay'){
	$post=get_postdata();
	$id=$post['id'];
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_bdd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$order['package']=json_decode($order['package'],true);
	$params['fee']=$order['money'];
	$params['title']='约课订单';
	$params['id']=$id;
	$params['plugin']='jy_bdd';
	//是否关闭余额支付
	if($return['basic']['iscredit']==1){
		$params['pay_type']='wechat';//可选，此项作用为指定支付方式
		$params['no_type']='credit';//可选，此项作用为指定不允许使用哪种支付方式
	}
	$params['back_url']='jy_bdd/order/payresult';
	$return['payid']=cfc('pay')->create_pay_order($params);
	exi($return);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	
	$order=pdo_get('jy_bdd_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	$order['package']=json_decode($order['package'],true);
	
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	pdo_update('jy_bdd_order',array('status'=>1,'pay'=>$payresult['fee']),array('id'=>$order['id']));
	exi('支付成功','success','jy_bdd/order/list/status=1');
}